/**
 * @MovieDirUtils.as
 *
 * @author sodaChen mail:sujun10@qq.com
 * @version 1.0
 * <br>Program Name:GameEngine
 * <br>Date:2016-8-25
 */
package com.asframe.utils
{
	import com.asframe.collections.maps.HashMap;
	import com.asframe.display.data.FrameData;
	import com.asframe.display.data.MovieData;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.geom.Rectangle;

	/**
	 * 动画图像工具
	 * @author sodaChen
	 * #Date:2016-8-25
	 */
	public class MovieDirUtils
	{
		/** 处理 **/
		private static var rect:Rectangle = new Rectangle();
		/**
		 * 解析一个方向的动作动画数据
		 * @param mc 资源mc
		 * @param dir 方向(0-7八个方向)
		 * @return 返回的是单个方向
		 */
		public static function parseDirBitmapMovie(mc:MovieClip,dir:int):MovieData
		{
			//生成资源图片
			var frames:Vector.<FrameData> = new Vector.<FrameData>();
			var bitmapDatas:Vector.<BitmapData> = new Vector.<BitmapData>();
			var pos:Object = null;
			var bitmapData:BitmapData  = null;
			//用于判断复制帧
			var bmpMap:HashMap = new HashMap();
			var frame:FrameData;
//			var rect:Rectangle;
			for (var i:int = 0; i < mc.posList.length; i++)
			{
				pos = mc.posList[i];
				frame = new FrameData();
				bitmapData =null;
				if(pos.key == "0")
				{

					bitmapData = RefUtils.refImage(mc.swfName + "_" + i,mc.loaderInfo.loader.contentLoaderInfo.applicationDomain);
				}
				else
				{
					bitmapData = bmpMap.get(pos.key);
					if(bitmapData == null)
					{
						bitmapData = RefUtils.refImage(mc.swfName + "_" + pos.key,mc.loaderInfo.loader.contentLoaderInfo.applicationDomain);
						bmpMap.put(pos.key,bitmapData);
					}
				}
				bitmapDatas.push(bitmapData);
//				rect = new Rectangle(pos.x,pos.y);
				rect.x = pos.x;
				rect.y = pos.y;
				if(bitmapData)
				{
					rect.width = bitmapData.width;
					rect.height = bitmapData.height;
				}
				frame.initFrames(bitmapData,rect);
				frames.push(frame);
			}
			return new MovieData(bitmapDatas,frames);
		}
		/**
		 * 解析BitmapMovie数据
		 * @param mc 资源mc
		 * @param frameName 动作名字
		 * @param isMap 是否为大地图
		 * @return 返回当前动作的各个方向的数据
		 */
		public static function parseBitmapMovie(mc:MovieClip,frameName:String,isMap:Boolean = false):Vector.<MovieData>
		{
			//生成资源图片
			var frames:Vector.<FrameData> = new Vector.<FrameData>();
			var bitmapDatas:Vector.<BitmapData> = new Vector.<BitmapData>();
			var pos:Object = null;
			var bitmapData:BitmapData  = null;
			//用于判断复制帧
			var bmpMap:HashMap = new HashMap();
			var frame:FrameData;
			var rect:Rectangle;
			var dirs:Vector.<MovieData>;
			
			if(mc["swfType"] && mc["swfType"] =="map")
			{
				var dirName:Array =["N","NE","E","ES","S","SW","W","WN"];
				dirs = new Vector.<MovieData>();
				var dir:String;
				for(var j:int =0;j<dirName.length;j++)
				{
					dir =dirName[j];
					var dirPosList:Array =mc[dir+"_posList"];
					var isReversal:Boolean = false;
					if(dirPosList.length == 0 && j<4)
					{
						//没有这个方向就反转
					
						dir =dirName[8-j];
						dirPosList = mc[dir+"_posList"];
						isReversal = true;
						
					}
					
					bitmapDatas =new Vector.<BitmapData>();
					frames =new Vector.<FrameData>();
					
				
					//野外角色
					for(var k:int=0;k<dirPosList.length;k++)
					{
						pos =dirPosList[k];
						bitmapData = RefUtils.refImage(dir+"_"+pos.key,mc.loaderInfo.loader.contentLoaderInfo.applicationDomain);
						frame = new FrameData();
						
						bitmapDatas.push(bitmapData);
						rect = new Rectangle(pos.x,pos.y);
						if(bitmapData)
						{
							rect.width = bitmapData.width;
							rect.height = bitmapData.height;
						}

						frame.initFrames(bitmapData,rect);
						frames.push(frame);

					}
					//trace("解析八方向:",frameName,dir,"索引",j);
					//是否需要反转
					if(isReversal)
						dirs.push(new MovieData(bitmapDatas,frames,frameName+"_"+dir,-1));
					else
						dirs.push(new MovieData(bitmapDatas,frames,frameName+"_"+dir));

				}


			}
			else
			{
				for (var i:int = 0; i < mc.posList.length; i++)
				{
					pos = mc.posList[i];
					frame = new FrameData();
					bitmapData =null;
					if(pos.key == "0")
					{

						bitmapData = RefUtils.refImage(mc.swfName + "_" + i,mc.loaderInfo.loader.contentLoaderInfo.applicationDomain);
					}
					else
					{
						bitmapData = bmpMap.get(pos.key);
						if(bitmapData == null)
						{
							bitmapData = RefUtils.refImage(mc.swfName + "_" + pos.key,mc.loaderInfo.loader.contentLoaderInfo.applicationDomain);
							bmpMap.put(pos.key,bitmapData);
						}
					}
					bitmapDatas.push(bitmapData);
					rect = new Rectangle(pos.x,pos.y);
					if(bitmapData)
					{
						rect.width = bitmapData.width;
						rect.height = bitmapData.height;
					}
					frame.initFrames(bitmapData,rect);
					frames.push(frame);
				}
				dirs = new Vector.<MovieData>();
				dirs.push(new MovieData(bitmapDatas,frames,frameName));
				dirs.push(new MovieData(bitmapDatas,frames,frameName,-1));
			}
			return dirs;
		}

		
	}
}